package di.scanner.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import di.scanner.mongo.dao.impl.*;
import di.scanner.mongo.model.ConsumerViewDI;
import di.scanner.mongo.model.MethodInvocationResult;
import di.scanner.mongo.model.ProviderViewDI;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UpdateDBUtil {

    public static void updateProviderViewDB() {
        ProviderViewDaoImpl providerViewDao = new ProviderViewDaoImpl();
        MethodInvocationResultDaoImpl methodInvocationResultDao = new MethodInvocationResultDaoImpl();

        List<ProviderViewDI> providerViewDIList = providerViewDao.search();
        Set<ProviderViewDI> newProviderViewDIList = new HashSet<>();
        List<MethodInvocationResult> methodInvocationResultList = methodInvocationResultDao.search();
        int unknowCount=0;
        for (ProviderViewDI providerViewDI : providerViewDIList) {
            boolean isMatch = false;
            String serviceType = providerViewDI.getServiceType();
            if ("WS - SOAP".equals(serviceType)) {
                String serviceName = providerViewDI.getServiceName();
                for (MethodInvocationResult methodInvocationResult : methodInvocationResultList) {
                    if (methodInvocationResult.getProviderClassName().contains(serviceName)) {
                        for (String methodName : methodInvocationResult.getProviderMethodNameList()) {
                            ProviderViewDI newProviderViewDI = JSONObject.parseObject(JSON.toJSONString(providerViewDI), ProviderViewDI.class);
                            newProviderViewDI.setMethodName(methodName);
                            newProviderViewDIList.add(newProviderViewDI);
                        }
                        isMatch = true;
                    }
                }
            } else if ("WS - RESTful".equals(serviceType)) {
                String url = providerViewDI.getServiceUrl();

                for (MethodInvocationResult methodInvocationResult : methodInvocationResultList) {
                    if (MethodInvocationResult.WS_RESTful.equals(methodInvocationResult.getServiceType())) {
                        if (methodInvocationResult.getUrl().contains(url)) {
                            for (String methodName : methodInvocationResult.getProviderMethodNameList()) {
                                ProviderViewDI newProviderViewDI = JSONObject.parseObject(JSON.toJSONString(providerViewDI), ProviderViewDI.class);
                                newProviderViewDI.setServiceDto(methodInvocationResult.getRestDto());
                                newProviderViewDI.setMethodName(methodName);
                                newProviderViewDIList.add(newProviderViewDI);
                            }
                            isMatch = true;
                        }

                    }
                }
            }else{
                unknowCount++;
            }
            if (!isMatch){
                ProviderViewDI newProviderViewDI = JSONObject.parseObject(JSON.toJSONString(providerViewDI), ProviderViewDI.class);
                newProviderViewDIList.add(newProviderViewDI);
            }
        }
        System.out.println(unknowCount);
        NewProviderViewDaoImpl newProviderViewDao = new NewProviderViewDaoImpl();
        for (ProviderViewDI providerView : newProviderViewDIList) {
            newProviderViewDao.save(providerView);
        }
    }

    public static void updateConsumerViewDB() {
        ConsumerViewDaoImpl consumerViewDao = new ConsumerViewDaoImpl();
        MethodInvocationResultDaoImpl methodInvocationResultDao = new MethodInvocationResultDaoImpl();
        List<ConsumerViewDI> consumerViewDIList = consumerViewDao.search();
        Set<ConsumerViewDI> newConsumerViewDIList = new HashSet<>();
        List<MethodInvocationResult> methodInvocationResultList = methodInvocationResultDao.search();
        ProviderViewDaoImpl providerViewDao = new ProviderViewDaoImpl();
        for (ConsumerViewDI consumerViewDI : consumerViewDIList) {
            boolean ismatch = false;
            String serviceType = consumerViewDI.getServiceType();
            if ("WS - SOAP".equals(serviceType)) {
                String serviceName = consumerViewDI.getServiceName();
                String consumerName = consumerViewDI.getcComponent();
                for (MethodInvocationResult methodInvocationResult : methodInvocationResultList) {
                    if (methodInvocationResult.getProviderClassName().contains(serviceName) && methodInvocationResult.getModuleName().equals(consumerName)) {
                        for (String methodName : methodInvocationResult.getProviderMethodNameList()) {
                            ConsumerViewDI newConsumerViewDI = JSONObject.parseObject(JSON.toJSONString(consumerViewDI), ConsumerViewDI.class);
                            newConsumerViewDI.setMethodName(methodName);
                            newConsumerViewDIList.add(newConsumerViewDI);
                        }
                        ismatch = true;
                    }
                }
            } else if ("WS - RESTful".equals(serviceType)) {
                String serviceName = consumerViewDI.getServiceName();
                ProviderViewDI providerViewDI = providerViewDao.searchByServerName(serviceName);
                if (providerViewDI != null) {
                    String url = providerViewDI.getServiceUrl();
                    for (MethodInvocationResult methodInvocationResult : methodInvocationResultList) {
                        if (methodInvocationResult.getServiceType().equals("WS - RESTful") && methodInvocationResult.getUrl().equals(url) && methodInvocationResult.getModuleName().equals(consumerViewDI.getcComponent())) {
                            for (String methodName : methodInvocationResult.getProviderMethodNameList()) {
                                ConsumerViewDI newConsumerViewDI = JSONObject.parseObject(JSON.toJSONString(consumerViewDI), ConsumerViewDI.class);
                                newConsumerViewDI.setMethodName(methodName);
                                newConsumerViewDIList.add(newConsumerViewDI);
                            }
                            ismatch = true;
                        }
                    }
                }
            }
            if (!ismatch){
                ConsumerViewDI newConsumerViewDI = JSONObject.parseObject(JSON.toJSONString(consumerViewDI), ConsumerViewDI.class);
                newConsumerViewDIList.add(newConsumerViewDI);
            }

        }

        NewConsumerViewDaoImpl newConsumerViewDao = new NewConsumerViewDaoImpl();
        for (ConsumerViewDI consumerViewDI : newConsumerViewDIList) {
            newConsumerViewDao.save(consumerViewDI);
        }

    }

    public static void main(String[] args) {
        updateProviderViewDB();
        updateConsumerViewDB();

    }
}
